
#include "sense_manager.h"
#include "communication.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "at24cxx.h"
#include <string.h>

#define Machine_PTOpr      0
#define InfraredTemp_PTOpr 1
#define Vibrator_PTOpr     2

static PT_SenseOpr g_ptSenseOprHead;
static PT_SenseOpr ptOprBuff[6];

int RegisterSenseOpr(PT_SenseOpr ptSenseOpr)
{
	PT_SenseOpr ptTmp;

	if (!g_ptSenseOprHead)
	{
		g_ptSenseOprHead   = ptSenseOpr;
		ptSenseOpr->ptNext = NULL;
	}
	else
	{
		ptTmp = g_ptSenseOprHead;
		while (ptTmp->ptNext)
		{
			ptTmp = ptTmp->ptNext;
		}
		ptTmp->ptNext	  = ptSenseOpr;
		ptSenseOpr->ptNext = NULL;
	}

	return 0;
}

PT_SenseOpr GetSenseOpr(char *pcName)
{
	PT_SenseOpr ptTmp = g_ptSenseOprHead;
	
	while (ptTmp)
	{
		if (strcmp(ptTmp->name, pcName) == 0)
		{
			return ptTmp;
		}
		ptTmp = ptTmp->ptNext;
	}
	return NULL;
}

void AllRegisterReport(uint32_t uibit)
{
	if ((uibit & (MACHINE_REGISTERBIT | LBS_REGISTERBIT)) != (MACHINE_REGISTERBIT | LBS_REGISTERBIT))
	{
		ptOprBuff[Machine_PTOpr]->RegisterReport();
	}
}

void All_SenseMemInit(void)
{
	PT_SenseOpr ptTmp = g_ptSenseOprHead ;
	while (ptTmp)
	{
		if(ptTmp->SenseMemInit != NULL)
		{
			ptTmp->SenseMemInit();
		}
		ptTmp = ptTmp->ptNext;
	}
}

int SenseInit(void)
{
	int iError;
	uint8_t i = 0;
	PT_SenseOpr ptTmp ;
	
	iError = MachineInit();
	iError |= InfraredTempInit();
	iError |= VibratorInit();
	
	ptTmp = g_ptSenseOprHead;
	while (ptTmp)
	{
		ptOprBuff[i++] = ptTmp;
		ptTmp = ptTmp->ptNext;
	}
	return iError;
}
